﻿<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>系统管理</title>
    <jsp:include page="/resources/inc/head.jsp" flush="true"/>
</head>
<body>
<div class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
        <!-- Panel Other -->
        <div class="ibox float-e-margins">
            <div class="ibox-content">
                <div class="row row-lg">
                    <div class="col-sm-12">
                        <!-- Example Events -->
                         <div class="example-wrap">
                            <div class="example">
                                <div class="btn-group hidden-xs mail-tools tooltip-demo" id="toolbar" role="group">
                                        <shiro:hasPermission name="upms:system:create"><button class="btn btn-white btn-sm" onclick="createAction()" title=""><i  class="glyphicon glyphicon-plus"></i>  新增</button></shiro:hasPermission>
                                </div>
                                <table id="table" data-height="400" data-mobile-responsive="true"></table>
                            </div>
                        </div>
                        <!-- End Example Events -->
                    </div>
                </div>
            </div>
        </div>
        <!-- End Panel Other -->
    </div>
</div>
    <div id="J_table-toolbar" style="display: none;">
        <shiro:hasPermission name="upms:system:update">
            <a class="update J_btn-table-operator" href="javascript:;" data-action="update" data-toggle="tooltip" title="Edit"><i class="glyphicon glyphicon-edit"></i></a>
        </shiro:hasPermission>
        <shiro:hasPermission name="upms:system:delete">
            <a class="delete J_btn-table-operator" href="javascript:;" data-action="delete" data-toggle="tooltip" title="Remove"><i class="glyphicon glyphicon-remove"></i></a>
        </shiro:hasPermission>
    </div>

<jsp:include page="/resources/inc/footer.jsp" flush="true"/>
<script>
    var $table = $('#table');
    var $tableToolbar = $('#J_table-toolbar');
    $(function () {
        // bootstrap table初始化
        $table.bootstrapTable({
            url: '${basePath}/manage/system/list',
            height: getHeight(),
            striped: true,
            search: true,
            showRefresh: true,
            showColumns: true,
            minimumCountColumns: 2,
            clickToSelect: false,
            detailView: false,
            pagination: true,
            paginationLoop: false,
            sidePagination: 'server',
            silentSort: false,
            smartDisplay: false,
            escape: true,
            searchOnEnterKey: true,
            idField: 'systemId',
            maintainSelected: true,
            toolbar: '#toolbar',
            columns: [
                {field: 'systemId', title: '编号', sortable: true, align: 'center'},
                {field: 'icon', title: '图标', sortable: true, align: 'center', formatter: 'iconFormatter'},
                {field: 'title', title: '系统标题'},
                {field: 'name', title: '系统名称'},
                {field: 'basepath', title: '根目录'},
                {field: 'status', title: '状态', sortable: true, align: 'center', formatter: 'statusFormatter'},
                {
                    field: 'action',
                    title: '操作',
                    align: 'center',
                    formatter: 'actionFormatter',
                    events: 'actionEvents',
                    clickToSelect: false
                }
            ]
        });




        $(document).on('click','.J_btn-table-operator',function () {
            var systemId  = $(this).attr('data-system-id');
            var action = $(this).attr('data-action');
            if('update' == action){
                updateAction(systemId);
            }else if ('delete' == action){
                deleteAction(systemId);
            }
        });



    });
    // 格式化操作按钮
    function actionFormatter(value, row, index) {
        $tableToolbar.find('.J_btn-table-operator').attr('data-system-id',row.systemId);
        return $tableToolbar.html();
    }
    // 格式化图标
    function iconFormatter(value, row, index) {
        return '<i class="' + value + '"></i>';
    }
    // 格式化状态
    function statusFormatter(value, row, index) {
        if (value == 1) {
            return '<span class="label label-success">正常</span>';
        } else {
            return '<span class="label label-default">锁定</span>';
        }
    }
    // 新增
    var createDialog;
    function createAction() {
        createDialog = $.dialog({
            animationSpeed: 300,
            title: '新增系统',
            content: 'url:${basePath}/manage/system/create',
            onContentReady: function () {
                initMaterialInput();
                initUploader();
            }
        });
    }
    // 编辑
    var updateDialog;
    function updateAction(systemId) {

        updateDialog = $.dialog({
            animationSpeed: 300,
            title: '编辑系统',
            content: 'url:${basePath}/manage/system/update/' + systemId,
            onContentReady: function () {
                initMaterialInput();
                initUploader();
            }
        });

    }
    // 删除
    var deleteDialog;
    function deleteAction(systemId) {
        deleteDialog = $.confirm({
            type: 'red',
            animationSpeed: 300,
            title: false,
            content: '确认删除该系统吗？',
            buttons: {
                confirm: {
                    text: '确认',
                    btnClass: 'waves-effect waves-button',
                    action: function () {
                        $.ajax({
                            type: 'get',
                            url: '${basePath}/manage/system/delete/' + systemId,
                            success: function (result) {
                                if (result.code != 1) {
                                    if (result.data instanceof Array) {
                                        $.each(result.data, function (index, value) {
                                            $.confirm({
                                                theme: 'dark',
                                                animation: 'rotateX',
                                                closeAnimation: 'rotateX',
                                                title: false,
                                                content: value.errorMsg,
                                                buttons: {
                                                    confirm: {
                                                        text: '确认',
                                                        btnClass: 'waves-effect waves-button waves-light'
                                                    }
                                                }
                                            });
                                        });
                                    } else {
                                        $.confirm({
                                            theme: 'dark',
                                            animation: 'rotateX',
                                            closeAnimation: 'rotateX',
                                            title: false,
                                            content: result.data.errorMsg,
                                            buttons: {
                                                confirm: {
                                                    text: '确认',
                                                    btnClass: 'waves-effect waves-button waves-light'
                                                }
                                            }
                                        });
                                    }
                                } else {
                                    deleteDialog.close();
                                    $table.bootstrapTable('refresh');
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                $.confirm({
                                    theme: 'dark',
                                    animation: 'rotateX',
                                    closeAnimation: 'rotateX',
                                    title: false,
                                    content: textStatus,
                                    buttons: {
                                        confirm: {
                                            text: '确认',
                                            btnClass: 'waves-effect waves-button waves-light'
                                        }
                                    }
                                });
                            }
                        });
                    }
                },
                cancel: {
                    text: '取消',
                    btnClass: 'waves-effect waves-button'
                }
            }
        });
    }
</script>
</body>
</html>